package com.test.cities.controller;

import com.test.cities.po.Comment;
import com.test.cities.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;

@Controller
public class BlogController {

    @Autowired
    private CommentService commentService;


    @GetMapping("/blog")
    public String index(Model model){
//        model.addAttribute("title","传递的Title");
//        model.addAttribute("description","传递的description");
//        model.addAttribute("keywords","传递的keywords");
        return "blog";
    }


    //提交评论表单，点击发布按钮走的post方法
    @PostMapping("/comments")
    public String post(Comment comment, HttpSession session) {
        //将评论信息储存到数据库
        commentService.saveComment(comment);
        //redirect到上面那个局部刷新的方法
        return "redirect:/comments/" ;
    }
    //该方法负责局部刷新页面的评论区
    @GetMapping("/comments/{blogId}")
    public String comments(@PathVariable Long blogId, Model model) {
        //将数据库里面所有的评论拿出来，回传给前端页面
        model.addAttribute("comments", commentService.listCommentByBlogId(blogId));
        //局部刷新的语法。前面是html页面，后面是th:fragment的值
        return "blog :: commentList";
    }

}
